﻿using CIS.Common.Entitys;
using CIS.Common.Enums;
using CIS.Server.EFCore;

namespace DataSeed
{
    internal class Program
    {
        private static CISServerContext? _dbContext;
        static void Main(string[] args)
        {
            _dbContext = new CISServerContext();
            Console.WriteLine("开始    向数据库插入数据");
            //InSertPortData();
            InSertUnitData();
            InSertRecipeData();
            //InSertInlineData();
            Console.WriteLine("结束    向数据库插入数据");
            Console.ReadKey();
        }

        private static void InSertRecipeData()
        {
            try
            {
                List<RecipeEntity> recipeEntity = new List<RecipeEntity>()
                {
                    new RecipeEntity
                    {
                        Id = Guid.NewGuid(),
                        SourceId="3AFS12-USC",
                        RecipeId="10",
                        RecipeNo=10,
                        Version=DateTime.Now.ToString(),
                        RecipeType="U"
                    },
                    new RecipeEntity
                    {
                        Id = Guid.NewGuid(),
                        SourceId="3AFS12-USC",
                        RecipeId="11",
                        RecipeNo=11,
                        Version=DateTime.Now.ToString(),
                        RecipeType="U"
                    },
                    new RecipeEntity
                    {
                        Id = Guid.NewGuid(),
                        SourceId="3AFS12-USC",
                        RecipeId="12",
                        RecipeNo=12,
                        Version=DateTime.Now.ToString(),
                        RecipeType="U"
                    },
                    new RecipeEntity
                    {
                        Id = Guid.NewGuid(),
                        SourceId="3AFS12-USC",
                        RecipeId="13",
                        RecipeNo=13,
                        Version=DateTime.Now.ToString(),
                        RecipeType="U"
                    },

                    new RecipeEntity
                    {
                        Id = Guid.NewGuid(),
                        SourceId="3AFS12-SPU",
                        RecipeId="1111",
                        RecipeNo=1111,
                        Version=DateTime.Now.ToString(),
                        RecipeType="U"
                    },
                    new RecipeEntity
                    {
                        Id = Guid.NewGuid(),
                        SourceId="3AFS12-SPU",
                        RecipeId="2222",
                        RecipeNo=2222,
                        Version=DateTime.Now.ToString(),
                        RecipeType="U"
                    },
                    new RecipeEntity
                    {
                        Id = Guid.NewGuid(),
                        SourceId="3AFS12-SPU",
                        RecipeId="3333",
                        RecipeNo=3333,
                        Version=DateTime.Now.ToString(),
                        RecipeType="U"
                    },
                    new RecipeEntity
                    {
                        Id = Guid.NewGuid(),
                        SourceId="3AFS12-SPU",
                        RecipeId="4444",
                        RecipeNo=4444,
                        Version=DateTime.Now.ToString(),
                        RecipeType="U"
                    },
                };
                _dbContext?.Set<RecipeEntity>().AddRangeAsync(recipeEntity);
                _dbContext?.SaveChanges();
                Console.WriteLine("向数据库插入 Recipe 数据 成功！");
            }
            catch
            {
                Console.WriteLine("向数据库插入 InlineTool 数据 失败！");
            }
        }

        private static void InSertPortData()
        {
            try
            {
                List<PortEntity> ports = new List<PortEntity>()

            {
                new PortEntity ()
                {
                    Id=Guid.NewGuid(),
                    PortId="P01",
                    Name="Port1",
                    QualifiedLocationId="3AFS12-IND-P01",
                    LocationId="1",
                    Status=PortStatus.Emty,
                    Enable=PortEnabled.Enabled,
                    PortUseType = PortUseType.OO,
                    PortType = PortType.PB,
                    TRSMode = TransferMode.AGVMode,
                    UnitPathNo=1,
                    RobotTeachingNo=1,
                    RobotName="3AFS12-IND",
                    UnitId="3AFS12-IND",
                    Capacity=30,
                    IsWIPConcerned=true,
                    CassetteSequenceNo=1001,
                },
                new PortEntity ()
                {
                    Id=Guid.NewGuid(),
                    PortId="P02",
                    Name="Port2",
                    QualifiedLocationId="3AFS12-IND-P02",
                    LocationId="2",
                    Status=PortStatus.Emty,
                    Enable=PortEnabled.Enabled,
                    PortUseType = PortUseType.OO,
                    PortType = PortType.PB,
                    TRSMode = TransferMode.AGVMode,
                    UnitPathNo=2,
                    RobotTeachingNo=2,
                    RobotName="3AFS12-IND",
                    UnitId="3AFS12-IND",
                    Capacity=30,
                    IsWIPConcerned=true,
                    CassetteSequenceNo=2001,
                },
                new PortEntity ()
                {
                    Id=Guid.NewGuid(),
                    PortId="P03",
                    Name="Port3",
                    QualifiedLocationId="3AFS12-IND-P03",
                    LocationId="3",
                    Status=PortStatus.Emty,
                    Enable=PortEnabled.Enabled,
                    PortUseType = PortUseType.OO,
                    PortType = PortType.PB,
                    TRSMode = TransferMode.AGVMode,
                    UnitPathNo=3,
                    RobotTeachingNo=3,
                    RobotName="3AFS12-IND",
                    UnitId="3AFS12-IND",
                    Capacity=30,
                    IsWIPConcerned=true,
                    CassetteSequenceNo=3001,
                }
            };
                _dbContext?.Set<PortEntity>().AddRangeAsync(ports.OrderBy(c=>c.PortId));
                _dbContext?.SaveChanges();
                Console.WriteLine("向数据库插入 Port 数据 成功！");
            }
            catch
            {
                Console.WriteLine("向数据库插入 Port 数据 失败！");
            }
        }

        private static void InSertInlineData()
        {
            try
            {
                List<InlineToolEntity> inlineTools = new List<InlineToolEntity>()
                {
                    new InlineToolEntity
                    {
                        Id = Guid.NewGuid(),
                        InlineToolId="3AFS12",
                        InlineType=InlineType.Normal,
                        EquipmentMode=EquipmentOperationMode.Normal,
                        OperationName="Normal",
                        Status=EquipmentStatus.Maint,
                        ControlState = CRST.OffLine,
                        ShopName=ShopName.ARRAY,
                        CanTpHalf=false
                    }
                };
                _dbContext?.Set<InlineToolEntity>().AddRangeAsync(inlineTools);
                _dbContext?.SaveChanges();
                Console.WriteLine("向数据库插入 InlineTool 数据 成功！");
            }
            catch
            {
                Console.WriteLine("向数据库插入 InlineTool 数据 失败！");
            }
        }

        private static void InSertUnitData()
        {
            try
            {
                List<UnitEntity> entity = new List<UnitEntity>()
                {
                    new UnitEntity
                    {
                        Id = Guid.NewGuid(),
                        UnitId="IND",
                        Name="IND",
                        QualifiedLocationId="3AFS12-IND",
                        Category=EquipmentType.BothIndexer,
                        LinkType=LinkType.EIPOrCCLink,
                        Status = EquipmentStatus.Maint,
                        UnitPathNo=200,
                        InlineToolId="3AFS12",
                        SoftwareVersion="20240516135900",                        
                    },
                    new UnitEntity
                    {
                        Id = Guid.NewGuid(),
                        UnitId="USC",
                        Name="USC",
                        QualifiedLocationId="3AFS12-USC",
                        Category=EquipmentType.Process,
                        LinkType=LinkType.EIPOrCCLink,
                        Status = EquipmentStatus.Maint,
                        UnitPathNo=300,
                        InlineToolId="3AFS12",
                        SoftwareVersion="20240516135900",
                    },
                    new UnitEntity
                    {
                        Id = Guid.NewGuid(),
                        UnitId="SPU",
                        Name="SPU",
                        QualifiedLocationId="3AFS12-SPU",
                        Category=EquipmentType.Process,
                        LinkType=LinkType.SECSII,
                        Status = EquipmentStatus.Maint,
                        UnitPathNo=400,
                        InlineToolId="3AFS12",
                        SoftwareVersion="20240516135900",
                    }
                };
                _dbContext?.Set<UnitEntity>().AddRangeAsync(entity);
                _dbContext?.SaveChanges();
                Console.WriteLine("向数据库插入 Unit 数据 成功！");
            }
            catch
            {
                Console.WriteLine("向数据库插入 Unit 数据 失败！");
            }
        }
    }
}
